﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using Expect.Core.Interfaces;
using Expect.Core.Html;

namespace Expect.Core.IO
{
    /// <summary>
    /// Specification writer for Html format
    /// </summary>
    public class HtmlSpecificationWriter : HtmlSpecification, ISpecificationWriter
    {
        /// <summary>
        /// Write a specification in a stream
        /// </summary>
        /// <param name="stream">
        /// The stream to write in
        /// </param>
        /// <param name="specification">
        /// The specification to write
        /// </param>
        /// <param name="renderExecutionResult">
        /// indicate if the execution result should be written
        /// </param>
        /// <param name="renderStandalone">
        /// indicate if the specification is rendered alone. If false, it means that the specification is rendered with other specification.
        /// If standalone, the specification must render with headers and footers
        /// </param>
        public void Write(Stream stream, SpecificationDefinition specification, bool renderExecutionResult, bool renderStandalone)
        {
            if (specification.Source is HtmlDocument)
            {
                GenerateAsInputed(stream, specification, renderExecutionResult, renderStandalone);
            }
            else
            {
                GenerateFromScratch(stream, specification, renderExecutionResult, renderStandalone);
            }
        }

        private void GenerateAsInputed(Stream stream, SpecificationDefinition specification, bool renderExecutionResult, bool renderStandalone)
        {
            HtmlDocument htmlDoc = specification.Source as HtmlDocument;

            foreach (var fixture in specification)
            {
                if (fixture.HasErrors)
                {

                }
                foreach (var row in fixture)
                {
                    foreach (var cell in row)
                    {
                        if (cell.Status.Success.HasValue)
                        {
                            if (cell.IsSuccessful)
                            {
                                HtmlNode node = cell.Source as HtmlNode;
                                node.Attributes.Append("style", "background-color:green");
                            }
                            else
                            {
                                HtmlNode node = cell.Source as HtmlNode;
                                node.Attributes.Append("style", "background-color:red");
                            }
                        }
                    }
                }
            }
            htmlDoc.Save(stream);
        }

        private void GenerateFromScratch(Stream stream, SpecificationDefinition specification, bool renderExecutionResult, bool renderStandalone)
        {
        }
    }
}
